<template>
    <inner-layout v-loading="loading">

        <div class="filter-bar clearfix">
            <div class="filter-item">
                <i class="el-icon-info"></i> 封闭基金总数：{{ dataTotal }}
            </div>
        </div>


        <div class="list-wrap">
            <el-table :data="dataList" style="width:100%">
                <el-table-column
                    prop="name"
                    label="指标">
                </el-table-column>
                <el-table-column
                    prop="top1"
                    label="折价率TOP1">
                </el-table-column>
                <el-table-column
                    prop="top2"
                    label="折价率TOP2">
                </el-table-column>
                <el-table-column
                    prop="top3"
                    label="折价率TOP3">
                </el-table-column>
                <el-table-column
                    prop="topNAvg"
                    :label="`前` + avgTopLimit + `平均折价率`">
                </el-table-column>
            </el-table>
        </div>

        <div class="tip-wrap">
            <div><i class="el-icon-info"></i>可转债数据来源：集思录</div>
            <div class="fr"><a href="https://www.jisilu.cn/data/cf/#stock" target="_blank">去集思录查看详情&gt;&gt;</a></div>
        </div>
    </inner-layout>
</template>

<script>
import { getJisiluCfList } from '@/api/jisilu'
export default {
	name: 'CfSummary',
	components: { },
    directives: { },
	mixins: [],
	data() {
		return {
			loading: false,
			dataList: [],
            dataTotal: 0,
            avgTopLimit: 0, //前N个平均值
		}
	},
	created() {
		this.getPageData()
	},
	methods: {
		getPageData() {
            let that = this
			that.loading = true
			getJisiluCfList().then((res) => {
                that.loading = false
				console.log('getPageData', res)
                let rows = res.data.rows || []
                if (rows.length > 0) {
                    that.dataTotal = rows.length
                    that.avgTopLimit = rows.length > 10 ? 10 : rows.length

                    let _discount_rt = [] //折价率
                    let _annualize_dscnt_rt = [] //年化折价率

                    rows.forEach(row => {
                        if (row.cell) {
                            _discount_rt.push(row.cell.discount_rt * 1)
                            _annualize_dscnt_rt.push(row.cell.annualize_dscnt_rt * 1)
                        }
                    })

                    _discount_rt.sort(function(a, b){return b - a})
                    _annualize_dscnt_rt.sort(function(a, b){return b - a})

                    let _index = 0
                    let _topNSum = 0

                    while(true) {
                        _topNSum += _discount_rt[_index] * 1
                        _index++
                        if (_index >= that.avgTopLimit) {
                            break
                        }
                    }

                    that.dataList.push({
                        name: '折价率',
                        top1: _discount_rt[0],
                        top2: _discount_rt[1],
                        top3: _discount_rt[2],
                        topNAvg: (_topNSum / _index).toFixed(3)
                    })

                    _index = 0
                    _topNSum = 0

                    while(true) {
                        _topNSum += _annualize_dscnt_rt[_index] * 1
                        _index++
                        if (_index >= that.avgTopLimit) {
                            break
                        }
                    }

                    that.dataList.push({
                        name: '年化折价率',
                        top1: _annualize_dscnt_rt[0],
                        top2: _annualize_dscnt_rt[1],
                        top3: _annualize_dscnt_rt[2],
                        topNAvg: (_topNSum / _index).toFixed(3)
                    })
                }
			})
		}
	}
}
</script>

<style lang="scss" scoped>
    .list-wrap {
        margin-top: 30px;
    }
    .tip-wrap {
        padding: 30px 0;
        font-size: 13px;
        line-height: 26px;
    }
</style>
